/**
 * @(#) CalendarProxy.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 국경일 및 휴일정보 Proxy - Proxy
 * 				월별 국경일 및 휴일 정보를 가져온다.
 */
package com.skt.common_hr.teamworktime.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.teamworktime.TeamWorkTimeFacade;
	import com.skt.common_hr.teamworktime.model.vo.DateVO;
	import com.skt.common_hr.teamworktime.model.vo.VacationVO;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class CalendarProxy extends CommonProxy
	{
		public static const NAME:String = "CalendarProxy";

		public var holidayList:ArrayCollection;
		public var vacationList:ArrayCollection;
		
		private static const STATUS_WAITING:String = "waiting";
		private static const STATUS_IDLE:String	= "idle";
		private var status:String = STATUS_IDLE;
		
		private var waitingPernr:String = "";
		private var waitingMonth:String = "";
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function CalendarProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번,대상월
		 * @return void
		 */
		public function load(pernr:String, month:String):void
		{
			if(status == STATUS_WAITING) {
				waitingPernr = pernr;
				waitingMonth = month;
				return;
			}

			var param:Object = new Object();
			param["I_PERNR"] 	= pernr;
			param["I_BEGDA"]	= DateUtil.toBasicString(firstDay(month));
			param["I_ENDDA"]	= DateUtil.toBasicString(lastDay(month));
			status = STATUS_WAITING;
			loadData("Z_HR_0023", param, result, fault); 
		}
		
		private function firstDay(month:String):Date
		{
			var baseDate:Date = new Date();
			baseDate.fullYear = parseInt(month.substr(0,4));
			baseDate.month = parseInt(month.substr(4, 2)) -1;
			baseDate.date = 1;
			return DateUtil.startOfWeek(baseDate);
		}
		
		
		private function lastDay(month:String):Date
		{
			var baseDate:Date = new Date();
			baseDate.fullYear = parseInt(month.substr(0,4));
			baseDate.month = parseInt(month.substr(4, 2))-1;
			baseDate.month ++;
			baseDate.date = 1;
			baseDate.date --;
			return DateUtil.endOfWeek(baseDate);
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0 || retCode==1) {  
				if(event.result.CALTAB1 == null) {
					event.result.CALTAB1 = new ArrayCollection();
				}
				else if(event.result.CALTAB1 is Array) {
					event.result.CALTAB1 = new new ArrayCollection(event.result.CALTAB1)
				}
				holidayList = parseHolidayList(event.result.CALTAB1);
				if(event.result.CALTAB2 == null) {
					event.result.CALTAB2 = new ArrayCollection();
				}
				if(event.result.CALTAB2 is Array) 
					vacationList = parseVacationList(new ArrayCollection(event.result.CALTAB2));
				else {
					vacationList = parseVacationList(event.result.CALTAB2);
				}
				
				////////////////////////////////////////////////////////////////////////
				// 2011.08.16		
				// 2011.12.23
				if(event.result.CALTAB4 == null) {
					event.result.CALTAB4= new ArrayCollection();
				}
				else if(!(event.result.CALTAB4 is ArrayCollection)) {
					event.result.CALTAB4 = new ArrayCollection(event.result.CALTAB4);
				}
				vacationList = new ArrayCollection(vacationList.toArray().concat(parseCeleVacationList(event.result.CALTAB4).toArray()));
				////////////////////////////////////////////////////////////////////////
				
								
				facade.sendNotification(TeamWorkTimeFacade.LOAD_CALENDAR_COMPLETE, this);
			}
			else {
				trace( "failed to CalendarProxy: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(TeamWorkTimeFacade.LOAD_CALENDAR_FAILED, this);
			}
			status = STATUS_IDLE;
			if(waitingPernr != "") {
				this.load(waitingPernr, waitingMonth);
				waitingPernr = waitingMonth = "";
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in DateProxy: " + event.fault.faultString);
			facade.sendNotification(TeamWorkTimeFacade.LOAD_CALENDAR_FAILED, this);
			status = STATUS_IDLE;
			if(waitingPernr != "") {
				this.load(waitingPernr, waitingMonth);
				waitingPernr = waitingMonth = "";
			}
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseHolidayList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:DateVO = new DateVO();
				vo.DATUM 		= DateUtil.stringToDateTime(rec["DATUM"]);
				vo.DATUM.seconds = vo.DATUM.minutes = vo.DATUM.milliseconds = vo.DATUM.hours = 0;
				vo.DTEXT		= rec["DTEXT"];
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		private function parseVacationList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				if(rec["WSTATUS"] != '3'){
					continue;
				}
				
				var vo:VacationVO = new VacationVO();
				
				vo.WGBN 		= rec["WGBN"];
				vo.WGBNT		= rec["WGBNT"];
				vo.DGBN			= rec["DGBN"];
				vo.DGBNT		= rec["DGBNT"];
				vo.AWART		= rec["AWART"];
				vo.AWATX		= rec["AWATX"];
				vo.BEGDA		= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA		= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.BEGUZ		= DateUtil.stringToDateTime(rec["BEGUZ"]);
				vo.ENDUZ		= DateUtil.stringToDateTime(rec["ENDUZ"]);
				vo.WSTATUS		= rec["WSTATUS"];
				vo.WSTATX		= rec["WSTATX"];
				//vo.TABNR		= rec["TABNR"];
				vo.TEXT1		= rec["TEXT1"];
				vo.GWAREKEY		= rec["GWAREKEY"];
				vo.OT011		= rec["OT011"];
				vo.OT012		= rec["OT012"];
				vo.OT021		= rec["OT021"];
				vo.OT022		= rec["OT022"];		
				
				retArr.addItem(vo);
			}
			return retArr;	
		}

		private function parseCeleVacationList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				if(rec["WSTATUS"] != '3'){
					continue; 
				}
				
				var vo:VacationVO = new VacationVO();
				
				if(StringUtil.NVL(rec.COBEG)!="" && StringUtil.NVL(rec.COEND)!="") {
					vo.WGBN 		= rec["WSTATUS"];
					vo.WGBNT		= rec["WSTATX"];
					vo.DGBN			= "1";
					vo.DGBNT		= rec["ZTEXT"];
					vo.AWART		= "1";
					vo.AWATX		= "경조휴가";
					vo.BEGDA		= DateUtil.stringToDateTime(rec["COBEG"]);
					vo.ENDDA		= DateUtil.stringToDateTime(rec["COEND"]);
					vo.WSTATUS		= rec["WSTATUS"];
					vo.WSTATX		= rec["WSTATX"];
					//vo.TABNR		= rec["TABNR"];
					vo.TEXT1		= rec["ZTEXT"];
					vo.GWAREKEY		= rec["GWAREKEY"];
					vo.isInsertFromCong = true;
					retArr.addItem(vo);
				}
			}
			return retArr;	
		}		
	}
}